//
//  UnlockVIew.m
//  手势解码
//
//  Created by wangcarl on 16/8/4.
//  Copyright © 2016年 wangcarl. All rights reserved.
//

#import "UnlockVIew.h"
@interface UnlockVIew()
@property (nonatomic,strong) NSMutableArray *selectedBTNs;
@property(nonatomic,assign) CGPoint currentPoint;

@end
@implementation UnlockVIew

- (NSMutableArray *)selectedBTNs
{
    if (_selectedBTNs==nil) {
        _selectedBTNs=[NSMutableArray array];
    }
    
    return _selectedBTNs;
}

-(void)awakeFromNib
{
    self.backgroundColor=[UIColor clearColor];

    
    for (int i=0; i<9; i++) {
        UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
        [button setBackgroundImage:[UIImage imageNamed:@"gesture_node_normal"] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageNamed:@"gesture_node_highlighted"] forState:UIControlStateSelected];
        button.tag=i;
        [self addSubview:button];
        button.userInteractionEnabled=NO;
    }
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    CGFloat w=74;
    CGFloat h=74;
    CGFloat x=0;
    CGFloat y=0;
    CGFloat marin=(self.bounds.size.width-3*w)/4;
    
    for (int i=0; i<9; i++) {
        int column=i%3;
        int row=i/3;
        x=marin+column*(marin+w);
        y=marin+row*(marin+h);
        UIButton *btn=self.subviews[i];
        btn.frame=CGRectMake(x, y, w, h);
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UITouch *touch=touches.anyObject;
    CGPoint point=[touch locationInView:self];
    for (int i=0; i<self.subviews.count; i++) {
        UIButton *btn=self.subviews[i];
        if (CGRectContainsPoint(btn.frame, point)&&!btn.selected) {
            btn.selected=YES;
            [self.selectedBTNs addObject:btn];
            break;
        }
        
    }
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UITouch *touch=touches.anyObject;
    CGPoint currentPoint=[touch locationInView:self];
    self.currentPoint=currentPoint;
    [self touchesBegan:touches withEvent:event];
    [self setNeedsDisplay];

}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    self.currentPoint=[self.selectedBTNs.lastObject center];
    NSMutableString *pwdString=[NSMutableString string];
    
    for (UIButton *btn in self.selectedBTNs) {
        [pwdString appendFormat:@"%@",@(btn.tag)];
    }

    [self setNeedsDisplay];
    
    if ([self.delegate respondsToSelector:@selector(UnLockView:withPwd:)]) {
        [self.delegate UnLockView:self withPwd:pwdString];
    }

    
    for (UIButton *btn in self.selectedBTNs) {
        btn.selected=NO;
    }
//    [self.selectedBTNs removeAllObjects];
    
    [self.selectedBTNs removeAllObjects];
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect {
    // Drawing code
    
    if (self.selectedBTNs.count==0) {
        return;
    }
    UIBezierPath *path=[UIBezierPath bezierPath];
    path.lineWidth=5;
    path.lineCapStyle=kCGLineCapRound;
    path.lineJoinStyle=kCGLineJoinRound;
    [[UIColor greenColor] set];
    for (int i=0; i<self.selectedBTNs.count; i++) {
        UIButton *btn=self.selectedBTNs[i];
        CGPoint center=btn.center;
        if (i==0) {
            [path moveToPoint:center];
        }else
        {
            [path addLineToPoint:center];
        }
    }
    [path addLineToPoint:self.currentPoint];
    
    [path stroke];
}
















@end
